package com.winshare.saleAssistant.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.*;

public class JXZYZXUtil {
    /**
     * 应用唯一标识
     */
    //private static String appID="ef6e6eee7920489cbec6c19e012dffd53";
    /**
     * 应用密钥
     */
    //private static String appSecret="51c595f8-e64e-4d03-a711-ddc925532332";

    /**
     * 获取access_token
     *
     * @param url
     * @return
     */
    public static JSONObject access_token(String url, String appID, String appSecret) {
        Map<String, String> param = new HashMap<>();
        param.put("client_id", appID);
        param.put("client_secret", appSecret);
        param.put("grant_type", "client_credentials");
        String response = HttpClientUtil.doGet(url + "/passport/access_token", param, null);
        return JSONObject.parseObject(response);
    }

    /**
     * 刷新access_token
     *
     * @param url
     * @return
     */
    public static JSONObject refresh_token(String url, String appID, String appSecret) {
        Map<String, String> param = new HashMap<>();
        param.put("client_id", appID);
        param.put("client_secret", appSecret);
        param.put("grant_type", "refresh_token");
        String response = HttpClientUtil.doGet(url + "/passport/refresh_token", param, null);
        return JSONObject.parseObject(response);
    }

    /**
     * 获得某个产品的全部模块清单
     *
     * @param url
     * @param produceCode  产品编码
     * @param access_token 授权令牌
     * @param appSecret    秘钥
     * @return
     */
    public static JSONObject getmodules(String url, String produceCode, String access_token, String appSecret) {
        if (produceCode.equals("P00001")) {
            Map<String, String> param = new HashMap<>();
            param.put("produceCode", produceCode);
            param.put("access_token", access_token);
            param.put("method", "getmodules");
            String response = HttpClientUtil.doPost(url + "/wsaApi/getData", param, null);
            return JSONObject.parseObject(response);
        } else {
            String date = DateUtil.formatDate(new Date(), "yyyy/MMdd HH:mm:ss");
            Map<String, String> param = new HashMap<>();
            param.put("produceCode", produceCode);
            param.put("method", "getmodules");
            param.put("pageNo", "1");
            param.put("pageSize", "10000");
            Map<String, String> headers = new HashMap<>();
            headers.put("date", date);
            headers.put("noncestr", RandomStringGenerator.getRandomStringByLength(32));
            //生成签名
            Map<String, String> signMap = param;
            signMap.put("date", date);
            signMap.put("noncestr", headers.get("noncestr"));
            headers.put("sign", getSign(signMap, appSecret));
            String response = HttpClientUtil.doGet(url, param, headers);
            return JSONObject.parseObject(response);

        }

    }

    /**
     * 获得某个产品的某块模块下的全部功能清单
     *
     * @param url
     * @param produceCode  产品编码
     * @param modelCode    模块编码
     * @param access_token 授权令牌
     * @return
     */
    public static JSONObject getfuncs(String url, String produceCode, String access_token, String modelCode, String appSecret) {
        if (produceCode.equals("P00001")) {
            Map<String, String> param = new HashMap<>();
            param.put("produceCode", produceCode);
            param.put("modelCode", modelCode);
            param.put("access_token", access_token);
            param.put("method", "getfuncs");
            String response = HttpClientUtil.doPost(url + "/wsaApi/getData", param, null);
            return JSONObject.parseObject(response);
        } else {
            String date = DateUtil.formatDate(new Date(), "yyyy/MMdd HH:mm:ss");
            Map<String, String> param = new HashMap<>();
            param.put("produceCode", produceCode);
            param.put("moduleCode", modelCode);
            param.put("method", "getfuncs");
            param.put("pageNo", "1");
            param.put("pageSize", "10000");
            Map<String, String> headers = new HashMap<>();
            headers.put("date", date);
            headers.put("noncestr", RandomStringGenerator.getRandomStringByLength(32));
            //生成签名
            Map<String, String> signMap = param;
            signMap.put("date", date);
            signMap.put("noncestr", headers.get("noncestr"));
            headers.put("sign", getSign(signMap, appSecret));
            String response = HttpClientUtil.doGet(url, param, headers);
            return JSONObject.parseObject(response);
        }

    }

    /**
     * 获得某个产品的帐号信息
     *
     * @param url
     * @param produceCode  产品编码
     * @param timestamp    时间戳
     * @param access_token 授权令牌
     * @return
     */
    public static JSONObject getaccounts(String url, String produceCode, String access_token, String timestamp, Integer pageNo, Integer pageSize, String appSecret) {
        if (produceCode.equals("P00001")) {
            Map<String, String> param = new HashMap<>();
            param.put("produceCode", produceCode);
            param.put("timestamp", timestamp);
            param.put("access_token", access_token);
            param.put("pageNo", pageNo + "");
            param.put("pageSize", pageSize + "");
            param.put("method", "getaccounts");
            String response = HttpClientUtil.doPost(url + "/wsaApi/getData", param, null);
            return JSONObject.parseObject(response);
        } else {
            String date = DateUtil.formatDate(new Date(), "yyyy/MMdd HH:mm:ss");
            Map<String, String> param = new HashMap<>();
            param.put("produceCode", produceCode);
            param.put("timestamp", timestamp);
            param.put("pageNo", pageNo + "");
            param.put("pageSize", pageSize + "");
            param.put("method", "getaccounts");
            Map<String, String> headers = new HashMap<>();
            headers.put("date", date);
            headers.put("noncestr", RandomStringGenerator.getRandomStringByLength(32));
            //生成签名
            Map<String, String> signMap = param;
            signMap.put("date", date);
            signMap.put("noncestr", headers.get("noncestr"));
            headers.put("sign", getSign(signMap, appSecret));
            String response = HttpClientUtil.doGet(url, param, headers);
            return JSONObject.parseObject(response);
        }
    }

    /**
     * 获得某个产品的用户登录日志
     *
     * @param url
     * @param produceCode  产品编码
     * @param timestamp    时间戳
     * @param access_token 授权令牌
     * @return
     */
    public static JSONObject getloginlogs(String url, String produceCode, String access_token, String timestamp, String beginDate, String endDate, Integer pageNo, Integer pageSize
            , String appSecret) {
        if (produceCode.equals("P00001")) {
            Map<String, String> param = new HashMap<>();
            param.put("produceCode", produceCode);
            param.put("timestamp", timestamp);
            param.put("access_token", access_token);
            param.put("pageNo", pageNo + "");
            param.put("pageSize", pageSize + "");
            param.put("method", "getloginlogs");
            if (ToolUtil.isNotEmpty(beginDate)) param.put("beginDate", beginDate);
            if (ToolUtil.isNotEmpty(beginDate)) param.put("endDate", endDate);
            String response = HttpClientUtil.doPost(url + "/wsaApi/getData", param, null);
            return JSONObject.parseObject(response);
        } else {
            String date = DateUtil.formatDate(new Date(), "yyyy/MMdd HH:mm:ss");
            Map<String, String> param = new HashMap<>();
            param.put("produceCode", produceCode);
            param.put("timestamp", timestamp);
            param.put("pageNo", pageNo + "");
            param.put("pageSize", pageSize + "");
            param.put("method", "getloginlogs");
            if (ToolUtil.isNotEmpty(beginDate)) param.put("beginDate", beginDate);
            if (ToolUtil.isNotEmpty(beginDate)) param.put("endDate", endDate);
            Map<String, String> headers = new HashMap<>();
            headers.put("date", date);
            headers.put("noncestr", RandomStringGenerator.getRandomStringByLength(32));
            //生成签名
            Map<String, String> signMap = param;
            signMap.put("date", date);
            signMap.put("noncestr", headers.get("noncestr"));
            headers.put("sign", getSign(signMap, appSecret));
            String response = HttpClientUtil.doGet(url, param, headers);
            return JSONObject.parseObject(response);

        }
    }

    /**
     * 获得某个产品的功能操作日志
     *
     * @param url
     * @param produceCode  产品编码
     * @param timestamp    时间戳
     * @param access_token 授权令牌
     * @return
     */
    public static JSONObject getuselogs(String url, String produceCode, String access_token, String timestamp, String beginDate, String endDate, Integer pageNo, Integer pageSize, String appSecret) {
        if (produceCode.equals("P00001")) {
            Map<String, String> param = new HashMap<>();
            param.put("produceCode", produceCode);
            param.put("timestamp", timestamp);
            param.put("access_token", access_token);
            param.put("pageNo", pageNo + "");
            param.put("pageSize", pageSize + "");
            param.put("method", "getuselogs");
            if (ToolUtil.isNotEmpty(beginDate)) param.put("beginDate", beginDate);
            if (ToolUtil.isNotEmpty(beginDate)) param.put("endDate", endDate);
            String response = HttpClientUtil.doPost(url + "/wsaApi/getData", param, null);
            return JSONObject.parseObject(response);
        } else {
            String date = DateUtil.formatDate(new Date(), "yyyy/MMdd HH:mm:ss");
            Map<String, String> param = new HashMap<>();
            param.put("produceCode", produceCode);
            param.put("timestamp", timestamp);
            param.put("pageNo", pageNo + "");
            param.put("pageSize", pageSize + "");
            param.put("method", "getuselogs");
            if (ToolUtil.isNotEmpty(beginDate)) param.put("beginDate", beginDate);
            if (ToolUtil.isNotEmpty(beginDate)) param.put("endDate", endDate);
            Map<String, String> headers = new HashMap<>();
            headers.put("date", date);
            headers.put("noncestr", RandomStringGenerator.getRandomStringByLength(32));
            //生成签名
            Map<String, String> signMap = param;
            signMap.put("date", date);
            signMap.put("noncestr", headers.get("noncestr"));
            headers.put("sign", getSign(signMap, appSecret));
            String response = HttpClientUtil.doGet(url, param, headers);
            return JSONObject.parseObject(response);
        }
    }

    /**
     * 获取签名
     *
     * @param appsecret 秘钥
     * @param map
     * @return
     */
    public static String getSign(Map<String, String> map, String appsecret) {
        ArrayList<String> list = new ArrayList<String>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (entry.getValue() != "") {
                list.add(entry.getKey() + "=" + entry.getValue() + "&");
            }
        }
        int size = list.size();
        String[] arrayToSort = list.toArray(new String[size]);
        Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(arrayToSort[i]);
        }
        String result = sb.toString();
        result += "key=" + appsecret;
        return DigestUtils.sha1Hex(result);
    }

    public static void main(String[] args) {
        //access_token1("http://wsa.cnhsqk.com/api","ef6e6eee7920489cbec6c19e012dffd53","51c595f8-e64e-4d03-a711-ddc925532332");
        System.out.println(DigestUtils.sha1Hex("date=2020/11/16 10:17:00&method=getmodules&noncestr=abc&produceCode=P00003&timestamp=0&appsecret=doidhfgkjidhxzknfkjd"));
    }

}
